/*
Copyright (c) 2008, Daniel Guerrero Martínez
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
	-Redistributions of source code must retain the above copyright notice, this
	list of conditions and the following disclaimer.
	-Redistributions in binary form must reproduce the above copyright notice,
	this list of conditions and the following disclaimer in the documentation
	and/or other materials provided with the distribution.
	-Neither the name of the <ORGANIZATION> nor the names of its contributors may
	be used to endorse or promote products derived from this software without
	specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
 */

package AudioVideo;

import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.io.IOException;
import javax.media.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;


public class Reproductor extends javax.swing.JInternalFrame {

    public Reproductor() {
        initComponents();
        JFileChooser dig = new javax.swing.JFileChooser();
        int resp = dig.showOpenDialog(this);
        if(resp == JFileChooser.APPROVE_OPTION){
            try {
                File f = dig.getSelectedFile();
                media = new javax.media.MediaLocator("file:"+f.getAbsolutePath());
                sonido = Manager.createPlayer(media);
                texto.setText(f.getName());
                sonido.start();
                sonido.addControllerListener(new miManejadorMedia(this));
            } catch (IOException ex) {
                JOptionPane err = new javax.swing.JOptionPane("El archivo seleccionado no es valido", JOptionPane.ERROR_MESSAGE);
                err.setVisible(true);
            } catch (NoPlayerException ex) {
                JOptionPane err = new javax.swing.JOptionPane("El formato del archivo no es valido", JOptionPane.ERROR_MESSAGE);
                err.setVisible(true);
            }
            this.setVisible(true);
            this.setSize(400, 100);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        texto = new javax.swing.JTextField();

        FormListener formListener = new FormListener();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        addInternalFrameListener(formListener);
        getContentPane().add(texto, java.awt.BorderLayout.PAGE_START);

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements javax.swing.event.InternalFrameListener {
        FormListener() {}
        public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            if (evt.getSource() == Reproductor.this) {
                Reproductor.this.cerrar_reproductor(evt);
            }
        }

        public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
        }
    }// </editor-fold>//GEN-END:initComponents

    private void cerrar_reproductor(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_cerrar_reproductor
        if(sonido != null)
            sonido.close();
    }//GEN-LAST:event_cerrar_reproductor


   void Anadir_controles(){
        areaVisual = sonido.getVisualComponent();
        if(areaVisual != null){
            this.add(areaVisual, BorderLayout.CENTER);
            this.setSize(600, 400);

        }
        panelControl = sonido.getControlPanelComponent();
        if(panelControl != null) this.add(panelControl, BorderLayout.SOUTH);
        validate();
    }

    class miManejadorMedia extends ControllerAdapter {
        Reproductor reproductor;

        miManejadorMedia(Reproductor reproductor) {
            this.reproductor = reproductor;
        }

        @Override
        public void controllerError(ControllerErrorEvent e) {
            sonido.close();
            if(areaVisual != null) remove(areaVisual);
            if(panelControl != null) remove(panelControl);
            validate();
            areaVisual = null;
            panelControl = null;
            sonido.removeControllerListener(this);
            sonido = null;
        }

        @Override
        public void realizeComplete(RealizeCompleteEvent e) {
            areaVisual = sonido.getVisualComponent();
            if(areaVisual != null){
                reproductor.add(areaVisual, BorderLayout.CENTER);
                reproductor.setSize(600, 400);
            }
            panelControl = sonido.getControlPanelComponent();
            if(panelControl != null) reproductor.add(panelControl, BorderLayout.SOUTH);
            validate();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField texto;
    // End of variables declaration//GEN-END:variables
    Player sonido;
    MediaLocator media;
    Component areaVisual;
    Component panelControl;
}
